package com.csun.nusing.familypresenter;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;

import com.csun.nusing.R;
import com.csun.nusing.presenter.BasePresenter;
import com.csun.nusing.presenter.inter.INursingMain;

/**
 * 护工端主界面的中间层
 * 处理radiogroup的点击
 * fragment的加载
 * ClassName: NursingMainPresenter 
 * @Description: TODO
 * @author fzq
 * @date 2017-10-20
 */
public class FamilyMainPresenter extends BasePresenter<INursingMain> {

	private FragmentTransaction transaction;
	private Fragment mCurrentFragment;

	public FamilyMainPresenter(INursingMain view) {
		super(view);
	}

	/**
	 * 处理RadioGroup
	 * 
	 * @Description: TODO
	 * @param @param bottomrg
	 * @return void
	 * @throws
	 * @author fzq
	 * @date 2017-10-19
	 */
	public void controlRadioGroup(RadioGroup bottomrg) {
		bottomrg.setOnCheckedChangeListener(new oncheckchange());
		bottomrg.check(R.id.activity_familymain_bottom_home_rb);
	}

	/**
	 * 处理Fragment的
	 * 
	 * @Description: TODO
	 * @param @param fragmentManager
	 * @param @param fragment
	 * @param @param name
	 * @return void
	 * @throws
	 * @author fzq
	 * @date 2017-10-19
	 */
	public void controlFragment(FragmentManager fragmentManager,
			Fragment fragment, String name) {
		transaction = fragmentManager.beginTransaction();
		if (mCurrentFragment != null) {
			/**
			 * 假如当前fragment是点击的fragment
			 */
			if (fragment == mCurrentFragment) {
				return;
			} else {
				/**
				 * 假如当前fragment不是点击的fragment,则隐藏当前fragment
				 */
				transaction.hide(mCurrentFragment);
			}
		}
		if (fragment.isAdded()) {
			transaction.show(fragment);
		} else {
			transaction.add(R.id.activity_familymain_fl, fragment, name);
			/**
			 * 第一次传值
			 */
		}
		transaction.commit();
		mCurrentFragment = fragment;
	}

	/**
	 * radioGroup的点击事件监听
	 * ClassName: oncheckchange 
	 * @Description: TODO
	 * @author fzq
	 * @date 2017-10-19
	 */
	private class oncheckchange implements OnCheckedChangeListener {

		@Override
		public void onCheckedChanged(RadioGroup group, int checkedId) {
			switch (checkedId) {
			case R.id.activity_familymain_bottom_home_rb:
				getViewRef().check(0);
				break;
			case R.id.activity_familymain_bottom_alarm_rb:
				getViewRef().check(1);
				break;
			case R.id.activity_familymain_bottom_oldster_rb:
				getViewRef().check(2);
				break;
			case R.id.activity_familymain_bottom_mine_rb:
				getViewRef().check(3);
				break;
			}
		}
	}

}
